package com.banciyuan.bcywebview.biz.detail.noveldetail.holder;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2017/3/2.
 */

public class ToolsHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private RelativeLayout pre_ll;
    private RelativeLayout next_ll;
    private TextView page_num;
    private ImageView ivnextImg;
    private ImageView ivPreImg;
    private TextView tvNextCharpter;
    private TextView tvPreCharpter;
    private LinearLayout tools_container;
    private View view_charpter;

    private boolean preClickable = true;
    private boolean nextClickable = true;

    public ToolsHolder(View itemView) {
        super(itemView);
        pre_ll = (RelativeLayout) itemView.findViewById(R.id.rl_precharpter);
        next_ll = (RelativeLayout) itemView.findViewById(R.id.rl_nextcharpter);
        page_num = (TextView) itemView.findViewById(R.id.novel_page_num);
        ivnextImg = (ImageView) itemView.findViewById(R.id.iv_nextimage);
        ivPreImg = (ImageView) itemView.findViewById(R.id.iv_preimage);
        tvNextCharpter = (TextView) itemView.findViewById(R.id.tv_nextcharpter);
        tvPreCharpter = (TextView) itemView.findViewById(R.id.tv_precharpter);
        view_charpter = itemView.findViewById(R.id.view_charpter);
        tools_container = (LinearLayout) itemView.findViewById(R.id.page_tool_bar);
    }

    public void initAction() {
        pre_ll.setOnClickListener(this);
        next_ll.setOnClickListener(this);
        page_num.setOnClickListener(this);
    }


    private void setPageNum(Complex novel) {
        if (null != novel.getSet_data()) {
            tools_container.setVisibility(View.VISIBLE);
            view_charpter.setVisibility(View.VISIBLE);
            page_num.setText(novel.getSet_data().getPost_pos() + "/" + novel.getSet_data().getCount());
        } else {
            tools_container.setVisibility(View.GONE);
            view_charpter.setVisibility(View.GONE);
        }
    }

    public void setData(Context mContext, Complex novel) {
        setPageNum(novel);
        if (null != novel.getSet_data()) {
            ColorStateList color1 = mContext.getResources().getColorStateList(R.color.mine_textcolor);
            ColorStateList color2 = mContext.getResources().getColorStateList(R.color.grey_ten_level);
            ColorStateList color3 = mContext.getResources().getColorStateList(R.color.font_color);
            ColorStateList color4 = mContext.getResources().getColorStateList(R.color.my_like_name_dark);
            boolean flag = SPHelper.getBoolean(mContext, SPConstant.SPNAME_CLOSELIGHT, SPConstant.NOVEL_LIGHT, true);

            if (novel.getSet_data().getPost_pos() < novel.getSet_data().getCount()) {
                ivnextImg.setImageResource(R.drawable.next_chapter_pink);
                if (flag) {
                    tvNextCharpter.setTextColor(color1);
                } else {
                    tvNextCharpter.setTextColor(color3);
                }
                nextClickable = true;
            } else {
                ivnextImg.setImageResource(R.drawable.next_chapter_gray);
                if (flag) {
                    tvNextCharpter.setTextColor(color2);
                } else {
                    tvNextCharpter.setTextColor(color4);
                }
                nextClickable = false;
            }
            if (novel.getSet_data().getPost_pos() > 1) {
                ivPreImg.setImageResource(R.drawable.prev_chapter_pink);
                if (flag) {
                    tvPreCharpter.setTextColor(color1);
                } else {
                    tvPreCharpter.setTextColor(color3);
                }
                preClickable = true;
            } else {
                ivPreImg.setImageResource(R.drawable.prev_chapter_grey);
                if (flag) {
                    tvPreCharpter.setTextColor(color2);
                } else {
                    tvPreCharpter.setTextColor(color4);
                }
                preClickable = false;
            }
        }
        initAction();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_precharpter:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.NOVEL_GO_PRE);
                break;
            case R.id.rl_nextcharpter:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.NOVEL_GO_NEXT);
                break;
            case R.id.novel_page_num:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.NOVEL_GO_INDEX);
                break;
        }
    }
}
